package com.songlanyun.modules.ship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.utils.OrderGen;
import com.songlanyun.common.utils.TypeCastUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.model.dto.ExpressShipDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.ship.dao.OrderPackagePickupDao;
import com.songlanyun.modules.ship.entity.OrderPackagePickup;
import com.songlanyun.modules.ship.service.OrderPackagePickupService;


@Service("orderPackagePickupService")
public class OrderPackagePickupServiceImpl extends ServiceImpl<OrderPackagePickupDao, OrderPackagePickup> implements OrderPackagePickupService {

    @Override
    public OrderPackagePickup create(OrderPackagePickup entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public OrderPackagePickup modify(OrderPackagePickup entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderPackagePickup> page = this.page(
                new Query<OrderPackagePickup>().getPage(params),
                new QueryWrapper<OrderPackagePickup>()
        );

        return new PageUtils(page);
    }

    @Override
    public OrderPackagePickup create(OrderPackage orderPackage, String  remark) {
        //创建订单自提包裹的信息
        OrderPackagePickup orderPackagePickup = new OrderPackagePickup();
        orderPackagePickup.setGoodsPackageId(orderPackage.getId());
        orderPackagePickup.setRemark(StringUtils.isNotBlank(remark)?remark:"");
        orderPackagePickup.setVerificationRole(OrderConstant.VerificationRole.NO);
        orderPackagePickup.setVerificationStatus(false);
        orderPackagePickup.setVerificationTime(null);
        orderPackagePickup.setVerificationCode(OrderGen.generateCheckNo());
        this.create(orderPackagePickup);
        return orderPackagePickup;
    }

    @Override
    public OrderPackagePickup loadByVerification(String verification) {
        OrderPackagePickup one = this.getOne(new LambdaQueryWrapper<OrderPackagePickup>()
                .eq(OrderPackagePickup::getVerificationCode, verification).last("limit 1"));
        return one;
    }
}